﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WinStudent
{
    public partial class FrmAddStudent : Form
    {
        public FrmAddStudent()
        {
            InitializeComponent();
        }

        private void FrmAddStudent_Load(object sender, EventArgs e)
        {
            rbNull.Checked = true;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string sno = txtSno.Text.Trim();
            string sname = txtSname.Text.Trim();
            string ssex = rbNull.Text;
            int sage = DateTime.Today.Year - dateTimePicker.Value.Year;
            string sdept = txtSdept.Text.Trim();

            if (string.IsNullOrEmpty(sno))
            {
                MessageBox.Show("学号不能为空", "添加学号提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (string.IsNullOrEmpty(sname))
            {
                MessageBox.Show("姓名不能为空", "添加姓名提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (rbBoy.Checked == true)
            {
                ssex = rbBoy.Text;
            }
            if (rbGirl.Checked == true)
            {
                ssex = rbGirl.Text;
            }
            if (rbNull.Checked == true)
            {
                ssex = rbNull.Text;
            }

            {
                string sqlSno = "select count(Sno) from Student where Sno=@Sno";
                SqlParameter[] paras =
                {
                    new SqlParameter("@Sno",sno)
                };
                object oSno = SqlHelper.ExecuteScalar(sqlSno, paras);
                if (oSno == null || oSno == DBNull.Value||(int)oSno==0)
                {
                    string sqlAdd = "Insert into Student (Sno,Sname,Ssex,Sage,Sdept) values (@Sno,@Sname,@Ssex,@Sage,@Sdept) ";
                    SqlParameter[] parasAdd =
                    {
                        new SqlParameter("@Sno",sno),
                        new SqlParameter("@Sname",sname),
                        new SqlParameter("@Ssex",ssex),
                        new SqlParameter("@Sage",sage),
                        new SqlParameter("@Sdept",sdept)
                    };
                    int count = SqlHelper.ExecutNonQuery(sqlAdd, parasAdd);

                    if (sage == 0)
                    {
                        string sqlSage = "update Student set Sage=null where Sage=0 ";
                        int countSage = SqlHelper.ExecutNonQuery(sqlSage);
                        if (countSage == 0)
                        {
                            MessageBox.Show("学生信息添加失败", "学生信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }

                    if (ssex == rbNull.Text)
                    {
                        string sqlSage = "update Student set Ssex=null where Ssex='空' ";
                        int countSsex= SqlHelper.ExecutNonQuery(sqlSage);
                        if (countSsex == 0)
                        {
                            MessageBox.Show("学生信息添加失败", "学生信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }

                    if (count>0)
                    {
                        MessageBox.Show("学生信息添加成功", "学生信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("学生信息添加失败", "学生信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }


                }
                else
                {
                    MessageBox.Show("学生信息已存在", "学生信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
